package com.lcb.manager.repository;


import com.lcb.manager.entity.Person;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface PersonRepository extends JpaRepository<Person, Integer>, JpaSpecificationExecutor<Person> {

    @Query(value = "select * from person where " +
            "(name like concat('%',:keyword,'%') or :keyword is NULL ) and " +
            "(address like concat('%',:area,'%') or :area is NULL) and " +
            "(sex = :sex or :sex is NULL) ",
            countQuery = "select count(*) from person where " +
                    "(name like concat('%',:keyword,'%') or :keyword is NULL ) and " +
                    "(address like concat('%',:area,'%') or :area is NULL) and " +
                    "(sex = :sex or :sex is NULL) ",
            nativeQuery = true)
    Page<Person> findByAllCondition(@Param("keyword") String keyword, @Param("area") String area, @Param("sex") Integer sex, Pageable pageable);


}
